package com.lujianfei.android_app_list.utils

import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import com.lujianfei.android_app_list.MyApplication
import com.lujianfei.android_app_list.bean.AppInfoBean


object AppHelper {

    fun getAppList(): List<AppInfoBean> {
        val applist = arrayListOf<AppInfoBean>()
        val pm: PackageManager = MyApplication.INSTANCE.packageManager
        // Return a List of all packages that are installed on the device.
        val packages = pm.getInstalledPackages(0)
        for (packageInfo in packages) { // 判断系统/非系统应用
            val applicationInfo = packageInfo.applicationInfo
            if (applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM == 0) {// 非系统应用
                applist.add(AppInfoBean(
                        appName = applicationInfo.loadLabel(pm).toString(),
                        appIcon = applicationInfo.loadIcon(pm),
                        packageName = packageInfo.packageName))
            }
        }
        return applist
    }
}